<?php

class ml_biz_search_gd {
    
    const BLOCK_SIZE = 40;
    
    private $url;
    private $aid;
    private $blockSum = 0;
    private $sum;
    
    private $num = self::BLOCK_SIZE;
    private $start = 0;
    private $searchKey;
    private $searchSort;
    
    
    const SEARCH_CONTENT_URL = "http://220.181.136.237/meila_gd/srm.php?qjson=";
    
    public function getSearchResult() {
        
        $this->initUrl();
        $rs = Tool_http::get($this->url);
        $rs = json_decode($rs, true);
        
        $this->sum = $rs['sp']['m2'];
        $this->blockSum = intval($this->sum/$this->num);
        
        $data = $rs['sp']['result'];
            
        $aUid = array();
        $aRid = array();
        foreach ($data as &$row){

            $row['content_rid'] = $row['rid'];
            $row['gd_price'] = $row['price'];
            $row['content'] = Tool_antiXss::clean_danger_param($row['content']);
            $row['content'] = strip_tags($row['content']);
            $row['gd_title'] = strip_tags($row['gd_title']);
            $row['type_xrank'] = ML_CONTENT_XRANK_GOODS;
            $aUid[] = $row['uid'];
            $aRid[] = $row['content_rid'];
            $aPid[] = $row['pic_id'];

        }
        $oBizPin = new ml_biz_pin_search();
        return $oBizPin ->output($data, $aUid, $aRid, $this->aid, $aPid);
        
    }
    
    public function __construct($keyword, $aid, $block = 0){
        $this->setKeyword($keyword);
        $this->aid = $aid;
        $this->setSearchBlock(intval($block));
        $this->setSearchSort();
    }
    
    private function setKeyword($keyword) {
        
        $keyword = trim($keyword);
        $keyword = mb_convert_encoding($keyword, 'GBK' , 'utf-8' );
        $this->keyword = urlencode($keyword);
    }
    
    public function get_total_block(){
        return intval($this->blockSum);
    }
    
    public function get_total_number(){
        return intval($this->sum);
    }
    
    public function setSearchSort($sort = 'hot') {
        if ($sort === 'new') {
            $this->searchSort = '+x_sort:digittime';
        } else {
            $this->searchSort = '+x_sort:hotrank';
        }
    }
    
    public function setSearchPrice() {
        
    }
    
    private function initUrl(){
        $this->url = self::SEARCH_CONTENT_URL. $this->keyword. $this->searchSort. '+x_where:status==1&&type==1&f=&num='. $this->num. '&start='. $this->start;
    }
    
    public function setSearchStart($start) {
        $this->start = intval($start);
    }
    
    public function setSearchNum($num) {
        $this->num = intval($num);
    }
    
    public function setSearchBlock($block){
        
        $this->start = $block*self::BLOCK_SIZE;
        
    }
    
    
}